package com.florent37.runtime_permission;

import ohos.aafwk.ability.fraction.Fraction;
import ohos.aafwk.content.Intent;
import ohos.utils.PacMap;
import ohos.global.icu.text.MessageFormat;

import java.util.ArrayList;
import java.util.List;

public class PermissionFraction extends Fraction {
    public static final String LIST_PERMISSIONS = "LIST_PERMISSIONS";

    private static final int REQUEST_CODE = 23;

    private List<String> permissionsList = new ArrayList<>();

    private PermissionListener listener;

    private PacMap mPacMap;

    public PermissionFraction() {

    }

    public static PermissionFraction newInstance(List<String> permissions) {
        final PacMap args = new PacMap();
        args.putStringList(LIST_PERMISSIONS, new ArrayList<String>(permissions));
        final PermissionFraction fraction = new PermissionFraction();
        fraction.setPacMap(args);
        return fraction;
    }

    public PacMap getPacMap() {
        return mPacMap;
    }

    public void setPacMap(PacMap mPacMap) {
        this.mPacMap = mPacMap;
    }

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        final PacMap arguments = getPacMap();
        if (arguments != null) {
            final List<String> permissionsArgs = arguments.getStringList(LIST_PERMISSIONS);
            if (permissionsArgs != null) {
                permissionsList.addAll(permissionsArgs);
            }
        }
    }

    @Override
    public void onActive() {
        if (permissionsList.size() > 0) {
            final String[] perms = new String[permissionsList.size()];
            permissionsList.toArray(perms);
            requestPermissionsFromUser(perms, REQUEST_CODE);
        } else {
            // this shouldn't happen, but just to be sure
            getFractionAbility().getFractionManager().startFractionScheduler()
                    .remove(this)
                    .submit();
        }
    }

    public void onRequestPermissionsResult(int requestCode, String[] permissions,int[] grantResults) {
        if (requestCode == REQUEST_CODE && permissions.length > 0 && this.listener != null) {
            final PermissionListener listener = this.listener;

            final List<String> acceptedPermissions = new ArrayList<>();
            final List<String> askAgainPermissions = new ArrayList<>();
            final List<String> refusedPermissions = new ArrayList<>();

            for (int i = 0; i < permissions.length; i++) {
                final String permissionName = permissions[i];
                if (grantResults[i] == getBundleManager().PERMISSION_GRANTED) {
                    acceptedPermissions.add(permissionName);
                } else {
//                    if (shouldShowRequestPermissionRationale(permissionName)) {
//                        //listener.onDenied(permissionResult);
//                        askAgainPermissions.add(permissionName);
//                    } else {
                        refusedPermissions.add(permissionName);
                        //listener.onForeverDenied(permissionResult);
//                    }
                }
            }
            listener.onRequestPermissionsResult(acceptedPermissions, refusedPermissions, askAgainPermissions);

            getFractionAbility().getFractionManager().startFractionScheduler()
                    .remove(this)
                    .submit();
        }
    }

    public PermissionFraction setListener(PermissionListener listener) {
        this.listener = listener;
        return this;
    }

    interface PermissionListener {
        void onRequestPermissionsResult(List<String> acceptedPermissions, List<String> refusedPermissions, List<String> askAgainPermissions);
    }
}
